<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-select
            clearable
            v-model="form.merchant_id"
            placeholder="商户"
            filterable
          >
            <el-option
              v-for="item in merchantList"
              :key="item.value"
              :label="item.name + '-' + item.merchant_id"
              :value="item.merchant_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item>
          <el-input placeholder="商户ID" v-model="form.merchant_id"></el-input>
        </el-form-item> -->
        <el-form-item>
          <el-select
            v-model="form.currency"
            placeholder="币种"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="item in currencyList"
              :key="item.name"
              :label="item.alias"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            clearable
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            v-model="form.date"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="merchant_id" label="商户id" align="center" />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
          />
          <el-table-column prop="date_at" label="日期" align="center" />
          <el-table-column prop="currency" label="币种" align="center" />
          <el-table-column prop="amount" label="金额" align="center">
            <template #default="scope">{{
              numberToCurrencyNo(scope.row.amount)
            }}</template>
          </el-table-column>
          <el-table-column prop="fee" label="费用" align="center">
            <template #default="scope">{{
              numberToCurrencyNo(scope.row.fee)
            }}</template>
          </el-table-column>
          <el-table-column prop="balance" label="统计时资产余额" align="center">
            <template #default="scope">{{
              numberToCurrencyNo(scope.row.balance)
            }}</template>
          </el-table-column>
          <el-table-column
            prop="start_at"
            label="开始时间"
            :formatter="formatterTime"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="end_at"
            label="截止时间"
            :formatter="formatterTime"
            align="center"
          ></el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from "vue";
import {
  formatterTime,
  exportOrDownload,
  numberToCurrencyNo,
} from "@/utils/utils";
import { statsPaymentListApi } from "@/api/ExAssistant/stats";
import { assetsCurrencyApi } from "@/api/ExAssistant/settings";
import { GlobalStore } from "@/store";

const globalStore = GlobalStore();
const merchantList = computed(() => globalStore.merchantList);
const loading = ref(false);
const tableData = ref([]);
const currencyList = ref([]);
const formData = {
  merchant_id: "",
  currency: "",
  date: [],
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
//参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
    currency: form.currency,
    date_at: form.date[0] || "",
    end_date_at: form.date[1] || "",
  };
};
// 获取币种
const getCurrencyList = async () => {
  try {
    const res = await assetsCurrencyApi({});
    currencyList.value = res.data.results;
  } catch (error) {
    currencyList.value = [];
  }
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await statsPaymentListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
//查询
const onQuery = async () => {
  getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1, pagesize: 9999999 });
  const res = await statsPaymentListApi(data, "blob");
  exportOrDownload(res, "PayFaz-付款统计列表");
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
  getCurrencyList();
});
</script>
